<?php
namespace app\manage\controller;

use think\Controller;
use think\Request;
use think\Validate;
use think\Db;
use Config;
use app\common\model\TokenCheck;

/**商铺房源设置
 * Class UnitBuilding
 * @package app\manage\controller
 */
class Shops extends Controller{
	/*public function __construct()
	{
		$this->token_check = new TokenCheck();
	}*/
    /**
     * 商铺房源显示
     * @return \think\response\Json
     */
    public function ShopsList(){
        $model=new \app\manage\model\Shops();
        $date=$model->index();
        returnJson(200,"成功",$date)->send();die;
    }

    /**
     * 商铺房源添加
     * @return \think\response\Json
     */
	public function ShopsAdd(){
        $model=new \app\manage\model\Shops();
        //get的时候显示后台设置的栋数和单元楼post的时候添加
        if(Request::instance()->isGet()){
            $data=$model->shopsList();
            //判断后台有没有设置单元楼栋数
            if($data==2){
                returnJson(-411,"请先在公寓设置里面设置")->send();die;
            }else{
                returnJson(200,"成功",$data)->send();die;
            }
        }else{
            $data = input();
            $validate = new Validate([
                ['s_tung_num', 'require|number', '请选择栋数|栋数必须是数字'],
                ['s_unit_num', 'require|number', '请选择单元|单元必须是数字'],
                ['s_layer_num', 'require|number', '请填写层数|层数必须是数字'],
                ['s_room_number', 'require|number', '请填写房号|房号必须是数字'],
                ['s_area', 'require|number', '请填写面积|单面积必须是数字'],
                ['s_face_width', 'require|number', '请选择面宽|面宽必须是数字'],
                ['s_layer_depth', 'require|number', '请选择层深|层深必须是数字'],
                ['s_depth', 'require|number', '请选择进深|进深必须是数字']
            ]);
            if(!$validate->check($data))
            {
                returnJson(-1001,  $validate->getError())->send();die;
            }
            //判断楼层房号是否重复
            $i=$model->tungs($data);
            if($i!=null){
                return json(["code"=>-416,"msg"=>'该单元楼层房号已存在']);
            }
            $date=$model->shopsAdd($data);
            if($date){
                returnJson(200,"成功")->send();die;
            }else{
                returnJson(-412,"添加失败")->send();die;
            }
        }
	}

    /**
     * 商铺房源修改
     * @return \think\response\Json
     */
    public function ShopsUpdate(){
        $model=new \app\manage\model\Shops();
        //get的时候显示需要修改的单元楼和单元楼post的时候修改
        if(Request::instance()->isGet()){
            $input = input();
            $h_id = trim( $input['shops_id'] );
            $data=$model->shopsOne($h_id);
            //var_dump($data['matching']);exit;
            $data['setting']=$model->shopsList($data['matching']);
            //判断后台有没有设置单元楼栋数
            if($data==null){
                returnJson(-413,"请选择正确的商铺")->send();die;
            }else{
                returnJson(200,"成功",$data)->send();die;
            }
        }else{
            $data = input();
            $validate = new Validate([
                ['s_tung_num', 'require|number', '请选择栋数|栋数必须是数字'],
                ['s_unit_num', 'require|number', '请选择单元|单元必须是数字'],
                ['s_layer_num', 'require|number', '请填写层数|层数必须是数字'],
                ['s_room_number', 'require|number', '请填写房号|房号必须是数字'],
                ['s_area', 'require|number', '请填写面积|单面积必须是数字'],
                ['s_face_width', 'require|number', '请选择面宽|面宽必须是数字'],
                ['s_layer_depth', 'require|number', '请选择层深|层深必须是数字'],
                ['s_depth', 'require|number', '请选择进深|进深必须是数字']
            ]);
            if(!$validate->check($data))
            {
                returnJson(-1001,  $validate->getError())->send();die;
            }
            //判断楼层房号是否重复
            $i=$model->tungsUp($data);
            if($i!=null){
                returnJson(-416,"该单元楼层房号已存在")->send();die;
            }
            $date=$model->shopsUpdate($data);
            if($date){
                returnJson(200,"成功")->send();die;
            }else{
                returnJson(-414,"修改失败")->send();die;
            }
        }
    }
    /**
     * 商铺房源删除
     * @return \think\response\Json
     */
    public function ShopsDel(){
        $model=new \app\manage\model\Shops();
        $input = input();
        $h_id = trim( $input['shops_id'] );
        $date=$model->ShopsDel($h_id);
        returnJson(200,"成功",$date)->send();die;
    }
    /**
     * 商铺房源删除
     * @return \think\response\Json
     */
    public function cs(){
        $input = input();
//        $type=json_encode($input['floor_type']);
//        $str_replace=str_replace('[','',$type);
//        $i=str_replace(']','',$str_replace);
//        var_dump($i);exit;


       $data= Db::name('unit_building')->where(['unit_id'=>5])->find();
       $u= json_decode($data['floor_type']);
       // $i[]=$data['floor_type'];
        $data['floor_type']=$u;
        returnJson(200,"成功",$data)->send();die;
        var_dump($u);exit;
        $date=implode(',',$input['type']);
        var_dump($input,$date);
    }
}